package net.Stratus.ImageDumper;

import java.util.ArrayList;
import java.util.Random;

import net.Stratus.ImageDumper.Sites.Default;

public class Job {
	// site config
	private Site site;
	private boolean canCount;
	private String identifier;
	// job config
	private int id;
	private long limit;
	private long kbLimit;
	private String url;
	private String directory;
	// progress info
	private int total;
	private int downloaded;
	private long kbDownloaded; 
	// other
	private static ArrayList<Job> jobs = new ArrayList<Job>();
	private static Random rand = new Random();
	
	/**
	 * Sets up a new Job
	 * 
	 * @param instance of site
	 * @param url url to dump from
	 * @param directory directory to dump to
	 */
	public Job(String url, String directory) {
		this(0, 0, url, directory);
	}
	
	/**
	 * Sets up a new job
	 *
	 * @param instance of site
	 * @param limit max number of pictures to be downloaded (no limit = 0)
	 * @param kbLimit max kilobytes to download (no limit = 0)
	 * @param url url to dump from
	 * @param directory directory to dump to
	 */
	public Job(long limit, long kbLimit, String url, String directory) {
		site = new Default(); // default until real site is set
		total = -1;
		downloaded = 0;
		kbDownloaded = 0;
		this.limit = limit;
		this.kbLimit = 0;
		this.url = url;
		this.directory = directory;
		// prevent two jobs from havign the same number
		boolean singular;
		do {
			singular = true;
			id = rand.nextInt(100000000-10000000)+10000000; // 8 digit number
			for(Job job : jobs) {
				if(id == job.id) {
					singular = false;
					break;
				}
			}
		} while(!singular);
			
		jobs.add(this); // add to joblist
		Logger.newLogger(id);
		
	}

	public void inturuptThread() {
		Thread.currentThread().interrupt();
	}
	
	// other gets/sets
	public static ArrayList<Job> getJobs() {
		return jobs;
	}
	// site config gets/sets
	public String getIdenftifier() {
		return identifier;
	}	
	public boolean canCount() {
		return canCount;
	}
	public Site getSite() {
		return site;
	}
	public void setSite(Site site, String identifier, boolean canCount) {
		this.site = site;
		this.identifier = identifier;
		this.canCount = canCount;	
	}
	// job config gets/sets
	public int getID() {
		return id;
	}
	public long getLimit() {
		return limit;
	}
	public void setLimit(long limit) {
		this.limit = limit;
	}
	public long getKBLimit() {
		return kbLimit;
	}
	public void setKBLimit(long kbLimit) {
		this.kbLimit = kbLimit;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getDirectory() {
		return directory;
	}
	public void setDirectory(String directory) {
		this.directory = directory;
	}
	// site info gets/sets
	public long getKBDownloaded() {
		return kbDownloaded;
	}
	public void addDownloaded() {
		downloaded++;
	}
	public void addDownloaded(int i) {
		downloaded += i;
	}
	public void addKBDownloaded(long kb) {
		kbDownloaded += kb;
	}
	public int getDownloaded() {
		return downloaded;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
}
